package com.eqdd.yiqidian.ui.yiqichuang;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.eqdd.yiqidian.ChuangMainActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.MyFragmentPagerAdapter;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.TabEntity;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.ui.yiqichuang.fragment.ChuangwenFragment;
import com.eqdd.yiqidian.ui.yiqichuang.fragment.ChuangwenMineFragment;
import com.eqdd.yiqidian.ui.yiqiyue.YueSearchActivity;
import com.eqdd.yiqidian.utils.ClickUtil;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 17-12-29  下午4:57.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class ChuangMainActivity extends BaseActivity {
    List<Fragment> mFragments;


    List<String> mTitles = new ArrayList<>();
    private ChuangMainActivityCustom dataBinding;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_chuang_main);
        initTopTitleBar(View.VISIBLE, "需求广场");
        initTopRightText("添加", v -> startActivityForResult(CreateChuangwenActivity.class, null, Config.CREATE_CHUANGWEN));
        dataBinding.includeTitle.ivSearch.setVisibility(View.VISIBLE);
        ClickUtil.click(dataBinding.includeTitle.ivSearch, () -> startActivity(ChuangSearchActivity.class));
    }

    @Override
    public void initData() {
        mTitles.add("创文圈");
        mTitles.add("视频圈");
        mTitles.add("需求广场");
        mTitles.add("我的");
        setupViewPager();
    }

    @Override
    public void setView() {

    }

    private void setupViewPager() {
        ArrayList<CustomTabEntity> customTabEntities = new ArrayList<>();
        mFragments = new ArrayList<>();
        for (int i = 0; i < mTitles.size() - 1; i++) {
            TabEntity tabEntity = new TabEntity(mTitles.get(i), 0, 0);
            customTabEntities.add(tabEntity);
            ChuangwenFragment yueFragment = new ChuangwenFragment();
            Bundle bundle = new Bundle();
            bundle.putString(Config.URL, HttpConfig.CHUANGWEN_LIST_RECENTLY);
            bundle.putInt(Config.IS_HOMEPAGE, 1);
            yueFragment.setArguments(bundle);
            mFragments.add(yueFragment);
        }
        customTabEntities.add(new TabEntity("我的", 0, 0));
        mFragments.add(new ChuangwenMineFragment());
        dataBinding.viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mTitles, mFragments));
        dataBinding.commonTabLayout.setTabData(customTabEntities);
        dataBinding.commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                dataBinding.viewPager.setCurrentItem(position);
                initTopTitleBar(View.VISIBLE, mTitles.get(position));

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        dataBinding.viewPager
                .addOnPageChangeListener(
                        new ViewPager.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                            }

                            @Override
                            public void onPageSelected(int position) {
                                dataBinding.commonTabLayout.setCurrentTab(position);
                                initTopTitleBar(View.VISIBLE, mTitles.get(position));
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {

                            }
                        }
                );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mFragments.get(dataBinding.viewPager.getCurrentItem()).onActivityResult(requestCode, resultCode, data);
    }
}
